using System;
using System.Collections;
using System.Diagnostics;

namespace Pegasus.Diagnostics
{
	/// <summary>
	/// 
	/// </summary>
	[Obsolete( "Use Log4Net API in place of TraceSwitches", false )]
	public class BooleanSwitchEx : BooleanSwitch
	{
		/// <summary>
		/// This list holds the BooleanSwitch.
		/// </summary>
		private static SortedList s_switchList = SortedList.Synchronized( new SortedList() );

		/// <summary>
		/// Initializes a new instance of the <see cref="T:BooleanSwitchEx"/> class.
		/// </summary>
		/// <param name="switchName">Name of the switch.</param>
		/// <param name="description">The description.</param>
		private BooleanSwitchEx( string switchName, string description ) : 
			base( switchName, description )
		{
		}

		/// <summary>
		/// Get a listed of the registed trace switches.
		/// </summary>
		public static SortedList BooleanSwitches
		{
			get
			{
				return (SortedList) s_switchList.Clone();
			}
		}

		/// <summary>
		/// This is a class factory method that will search the list for an existing switch
		/// with the specified display name, if we find it we return the object.  If one isn't 
		/// there, a new one is allocated, placed in the list, and returned to the caller.  
		/// </summary>
		/// <param name="switchName">The name of the switch.</param>
		/// <param name="description">The description for the switch object.</param>
		/// <returns>
		/// A valid switch object is returned to the caller.  Either one that was found in the 
		/// list or a new one that was just allocated.
		/// </returns>
		public static BooleanSwitchEx AllocateBooleanSwitch( string switchName, string description )
		{
			BooleanSwitchEx boolSwitch = (BooleanSwitchEx) s_switchList[ switchName ];
			if( boolSwitch == null )
			{
				boolSwitch = new BooleanSwitchEx( switchName, description );
				s_switchList.Add( switchName, boolSwitch );
			}

			return boolSwitch;
		}

		/// <summary>
		/// Use this static method if you want to remove a switch from control of this 
		/// object.  This should be use very INFREQUENTLY. It will remove the item from 
		/// the list no matter how many references to the switch have been handed out.  
		/// For the most part, simply allocate a switch as a private static item and forget 
		/// about it.  This method is provided for the very few cases when it really should 
		/// be removed once you are finished with it. An ArgumentException is thrown if the 
		/// switch is not found in the list
		/// </summary>
		/// <param name="switchName">The name of the switch to remove.</param>
		public static void RemoveBooleanSwitch( string switchName )
		{
			// Remove the item.
			s_switchList.Remove( switchName );
		}

		/// <summary>
		/// Use this static method to get the current value of an switch object by passing in 
		/// the switch name.  This method is synchronized (locked on the switch class type) to 
		/// protect the hash table operations.
		/// </summary>
		/// <param name="switchName">The switch name to get.</param>
		/// <returns>The TraceSwitch for the given name.</returns>
		public static BooleanSwitchEx GetBooleanSwitch( string switchName )
		{
			return (BooleanSwitchEx) s_switchList[ switchName ];
		}
	}
}
